import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/home/convenient/bill/bill_page_controller.dart';
import 'package:kkhousekeep/widget/custom_app_bar.dart';
import 'package:kkhousekeep/widget/custom_picker.dart';

import '../../../../models/home_bill_list_entity.dart';

class BillPage extends StatelessWidget {
  const BillPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: BillPageController(),
        builder: (controller) => Container(
              decoration: const BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage("assets/images/page_bg.png"),
                      fit: BoxFit.fill)),
              child: Scaffold(
                  backgroundColor: Colors.transparent,
                  appBar: CustomAppBar(
                    "物业账单",
                    backgroundColor: Colors.transparent,
                  ),
                  body: Stack(
                    alignment: AlignmentDirectional.topCenter,
                    children: [
                      _buildTopIcon(controller, context),
                      _buildCenterItems(controller),
                    ],
                  )),
            ));
  }

  //顶部组件
  Widget _buildTopIcon(BillPageController controller, BuildContext context) {
    return Positioned(
        top: 66.h,
        child: Container(
          height: 156.h,
          width: 666.w,
          padding: EdgeInsets.only(bottom: 16.h),
          alignment: Alignment.center,
          decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage(
                    "assets/images/convenient_bill_year_picker_icon.png")),
          ),
          child: InkWell(
            onTap: () => _showBottomSheetDialog(controller),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Obx(() => Text(
                      controller.years[controller.yearIndex.value],
                      style: TextStyle(fontSize: 36.sp, color: Colors.white),
                    )),
                SizedBox(
                  width: 18.w,
                ),
                Image.asset(
                  "assets/images/convenient_bill_drop_down_icon.png",
                  width: 24.h,
                  height: 19.86.h,
                )
              ],
            ),
          ),
        ));
  }

  //中部列表
  Widget _buildCenterItems(BillPageController controller) {
    return Positioned(
      top: 238.h,
      width: 1.sw,
      child: Obx(
        () => Container(
          height: 1236.h,
          padding: EdgeInsets.only(top: 38.h, left: 32.w, right: 32.w),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(48.w),
                  topRight: Radius.circular(48.w)),
              color: controller.getCurrentBillList().isEmpty
                  ? const Color(0xFFF4F5F6)
                  : Colors.white),
          child: controller.getCurrentBillList().isEmpty
              ? _buildNoneState()
              : ListView.builder(
                  itemCount: controller.getCurrentBillList().length,
                  itemBuilder: (context, index) => _buildSingleItem(
                      controller.getCurrentBillList()[index], controller),
                ),
        ),
      ),
    );
  }

  //单个列表项
  Widget _buildSingleItem(
      HomeBillListObj entity, BillPageController controller) {
    return InkWell(
        onTap: () => controller.performClickItem(entity),
        child: Container(
          height: 132.h,
          decoration: BoxDecoration(
              border: Border(
                  bottom:
                      BorderSide(width: 4.h, color: const Color(0xFFF2F1F6)))),
          child: Flex(
            direction: Axis.horizontal,
            children: [
              Expanded(
                  flex: 18,
                  child: Container(
                    padding: EdgeInsets.only(right: 20.w),
                    alignment: Alignment.centerRight,
                    child: _customText(
                        '${entity.month}${BillPageController.unitMonth}'),
                  )),
              Expanded(
                flex: 72,
                child: Container(
                  alignment: Alignment.centerLeft,
                  child: _customText("￥${entity.money.toStringAsFixed(2)}"),
                ),
              ),
              Expanded(flex: 10, child: _buildJumpIcon())
            ],
          ),
        ));
  }

  Widget _customText(String content) {
    return Text(content,
        style: TextStyle(fontSize: 32.sp, color: const Color(0xFF6D6D6D)));
  }

  Widget _buildJumpIcon() {
    return Image.asset(
      "assets/images/icon_segue_arrow_grey.png",
      width: 48.h,
      height: 48.h,
    );
  }

  void _showBottomSheetDialog(BillPageController controller) {
    for(var yearBillMap in controller.yearsBillMaps.values){
      if(yearBillMap.isEmpty){
        return;
      }
    }
    Get.bottomSheet(
      CustomPick(
        pickList: controller.years,
        confirmCallBack: (index) => controller.performClickDialogConfirm(index),
        index: controller.yearIndex.value,
      ),
    );
  }

  Widget _buildNoneState() {
    return Container(
      decoration: const BoxDecoration(
        color: Color(0xFFF4F5F6),
      ),
      child: Stack(
        alignment: Alignment.center,
        children: [
          Positioned(
            top: 272.h,
            child: Image.asset(
              "assets/images/home_bill_none_state_icon.png",
              width: 288.w,
              height: 358.w,
            ),
          ),
          Positioned(
            top: 638.h,
            child: Text(
              "没有找到你的账单",
              style: TextStyle(
                fontSize: 28.sp,
                color: const Color(0xFF999999),
              ),
            ),
          )
        ],
      ),
    );
  }
}
